<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\my\MyPage;
use app\admin\my\MyConfig;


class Node extends Base
{
   
    public function node()
    {
        $my = new MyPage();
        $my->title('权限列表') ->setDb('AdminNode')->buttonNew("nodeEdit")
            ->keyId()
            ->keyIcon()
            ->keyText('node_name', '名称')
            ->keyJoin('typeid', '父级', 'id', 'node_name', 'admin_node')
            ->keyText('module_name', 'module')
            ->keyText('control_name', 'control')
            ->keyText('action_name', 'action')
            ->keyMap('is_menu', '菜单', ['否','是'])
            ->keyText('sort', '排序')
           
            ->actionEdit('nodeEdit?id=###', '编辑')
            ->Action('nodeEdit?id=###', '删除');


       if ( request()->isAjax() ) {
            return myData($my,'AdminNode',['typeid'=>0]);
        } else {
            return $my->fetch();
       }

    }
    
    public function nodeEdit($id='')
    {
        
        if(request()->isPost()){
            $res = model('Node')->editData();
            if ($res) $this->success('更新成功','node');
            $this->error('更新失败','node');
        
        } else {
            $data=myEdit('Admin/Node',$id);
            $nodes = db('admin_node')->field(true)->select();
            $nodes = model('common/Tree')->toFormatTree($nodes,'node_name','id','typeid');
            $nodes = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')), $nodes);
            $nodes =array_column($nodes, 'title_show', 'id');
            

            $my = new MyConfig();
            return $my->title('编辑节点')
                ->keyId()
                ->keyText('node_name', '节点')
                ->keyText('module_name', '模块')
                ->keyText('control_name', '控制器')
                ->keyText('action_name', '操作')
                ->keySelect('is_menu', '菜单显示', '','', ['否',1=>'是'])
                ->keySelect('typeid', '父节点', '','', $nodes)
                ->data($data)
                ->fetch();
        }
    }

    

    
    public function giveAccess()
    {
        $param = input('param.');
        $node = new Node();
        //获取现在的权限
        if('get' == $param['type']){

            $nodeStr = $node->getNodeInfo($param['id']);
            return json(['code' => 1, 'data' => $nodeStr, 'msg' => 'success']);
        }
        //分配新权限
        if('give' == $param['type']){

            $doparam = [
                'id' => $param['id'],
                'rule' => $param['rule']
            ];
            $user = new UserType();
            $flag = $user->editAccess($doparam);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }
}